﻿// ***********************************************************************
// Assembly         : OccExample.Commands
// Author           : TianTeng
// Created          : 2024/4/22 11:54:59
//
// Last Modified By : TianTeng
// Last Modified On : 2024/4/22 11:54:59
// ***********************************************************************
// <copyright file="StepToGltfCmd.cs" company="">
//     Copyright ©  2024
// </copyright>
// <summary></summary>
// ***********************************************************************
using Occt;
using System;
using Tx;

namespace OccExample.Commands
{
    /// <summary>
    /// Class StepToGltfCmd.
    /// </summary>
    /// <seealso cref="IExtendCommand" />
	public class StepToGltfCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        { // read / create / fill in the document
            XCAFApp_Application.Application.NewDocument("MDTV-XCAF", out var theXdeDoc);
            var aStepReader = new STEPCAFControl_Reader();
            if (aStepReader.ReadFile("D:\\A-0017072.STEP") == IFSelect_ReturnStatus.IFSelect_RetDone)
            {
                if (aStepReader.Transfer(theXdeDoc))
                {
                    // collect document roots into temporary compound
                    var aShapeTool = XCAFDoc_DocumentTool.ShapeTool(theXdeDoc.Main);

                    aShapeTool.GetFreeShapes(out var aRootLabels);

                    var aBuildTool = new BRep_Builder();
                    aBuildTool.MakeCompound(out var aCompound);
                    foreach (var rootLabel in aRootLabels)
                    {
                        if (XCAFDoc_ShapeTool.GetShape(rootLabel, out var aRootShape))
                        {
                            aBuildTool.Add(aCompound, aRootShape);
                        }
                    }

                    // perform meshing
                    //Handle(Prs3d_Drawer) aDrawer = new Prs3d_Drawer(); // holds visualization defaults
                    var anAlgo = new BRepMesh_IncrementalMesh();
                    anAlgo.ChangeParameters.Deflection = 0.01;//Prs3d::GetDeflection(aCompound, aDrawer);
                    anAlgo.ChangeParameters.Angle = 20.0 * Math.PI / 180.0; // 20 degrees
                    anAlgo.ChangeParameters.InParallel = true;
                    anAlgo.Shape = aCompound;
                    anAlgo.Perform();

                    // write or export the document
                    var aMetadata = new TColStd_IndexedDataMapOfStringString();
                    //RWGltf_CafWriter aGltfWriter("d:\\test.glb", true);
                    var aGltfWriter = new RWGltf_CafWriter(new TCollection_AsciiString("d:\\test.gltf"), false);
                    // STEP reader translates into mm units by default
                    aGltfWriter.ChangeCoordinateSystemConverter.InputLengthUnit = 0.001;
                    aGltfWriter.ChangeCoordinateSystemConverter.SetInputCoordinateSystem(RWMesh_CoordinateSystem.RWMesh_CoordinateSystem_Zup);
                    var theProgress = new Message_ProgressRange();
                    if (!aGltfWriter.Perform(theXdeDoc, aMetadata, theProgress))
                    {
                        // export error 
                    }
                }
            }

            return CmdResult.Succeed;
        }
    }
}
